﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;

namespace MvcClient.Pages.Account
{
    public class LoginModel : PageModel
    {
        private readonly ILogger<LoginModel> _logger;
        protected readonly IAuthenticationSchemeProvider _schemes;
        protected readonly IAuthenticationHandlerProvider _handlers;
        private readonly IOptions<AuthenticationOptions> _authOptions;

        public LoginModel(

             IAuthenticationSchemeProvider schemes,

             IAuthenticationHandlerProvider handlers,

             IOptions<AuthenticationOptions> options,
             ILogger<LoginModel> logger)
        {  
            _logger = logger;
            _authOptions = options;
            _schemes = schemes;
            _handlers = handlers;
        }

        //public IActionResult OnGet(string returnUrl = null)
        //{
        //    //var redirectUrl = Url.Page("./ExternalLogin", pageHandler: "Callback", values: new { returnUrl });
        //    //var properties = _signInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl);
        //    //return new ChallengeResult(provider, properties);
        //    return new ChallengeResult();
        //}

        public async Task<IActionResult> OnGetAsync(string returnUrl = null)
        {
            //returnUrl = returnUrl ?? Url.Content("~/");
            //// Clear the existing external cookie to ensure a clean login process
            //await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
            //ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
            //// ===================================

            //ReturnUrl = returnUrl;
            // IEnumerable<AuthenticationScheme>  IEnumerable<AuthenticationScheme>

            //var allSchemesAsync = await this._schemes.GetAllSchemesAsync();
            //var authenticationSchemes = allSchemesAsync.Where(s => !string.IsNullOrEmpty(s.DisplayName)).ToList();

            //string ss = "";
            //foreach (var scheme in await _schemes.GetAllSchemesAsync())
            //{
            //    ss += scheme.Name + Environment.NewLine;
            //    _logger.Log(LogLevel.Information, $"Scheme: {scheme.Name}, Handler: {scheme.HandlerType.FullName}");
            //}

            //foreach (var scheme in authenticationSchemes)
            //{
            //    ss += scheme.Name + Environment.NewLine;
            //    _logger.Log(LogLevel.Information, $"Scheme: {scheme.Name}, Handler: {scheme.HandlerType.FullName}");
            //}
            var allSchemes = await this._schemes.GetAllSchemesAsync();
            var scheme = allSchemes.FirstOrDefault(s => !string.IsNullOrEmpty(s.DisplayName));
            if(scheme == null)
            {
                throw new ArgumentNullException("scheme", "没有添加认证方式");
            }

            returnUrl = returnUrl ?? Url.Content("~/");
            var  authenticationProperty = new AuthenticationProperties()
            {
                RedirectUri = returnUrl
            };
            authenticationProperty.Items["LoginProvider"] = scheme.Name; // oidc
            return new ChallengeResult(scheme.Name, authenticationProperty);
        }
    }
}